package snake;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class SnakePanel extends JPanel implements KeyListener,ActionListener{
	ImageIcon up = new ImageIcon("up.png");
	ImageIcon down = new ImageIcon("down.png");
	ImageIcon right = new ImageIcon("right.png");
	ImageIcon left = new ImageIcon("left.png");
	ImageIcon title = new ImageIcon("title.jpg");
	ImageIcon food = new ImageIcon("food.png");
	ImageIcon body = new ImageIcon("body.png");
	
	int[] snakex = new int[750];
	int[] snakey = new int[750];
	Random rand = new Random();
	int foodx = rand.nextInt(34)*25 + 25;
	int foody =rand.nextInt(24)*25 +75;  
	int len = 3;
	int score = 0;
	String direction = "R";
	
	boolean isStarted = false ;
	boolean isFailed = false;
	boolean keyStop = true;
	Timer timer = new Timer(100,this);
	
	public SnakePanel() {
		this.setFocusable(true);
		this.addKeyListener(this);
		setup();
//		timer.start();
	}
	
	public void paint(Graphics g) {
		super.paint(g);
		this.setBackground(Color.WHITE);
		title.paintIcon(this, g, 25, 11);
		g.fillRect(25, 75, 850, 600);
		
		//画蛇头
		if(direction.equals("R")){
			right.paintIcon(this, g, snakex[0], snakey[0]);
		}else if(direction.equals("L")){
			left.paintIcon(this, g, snakex[0], snakey[0]);
		}else if(direction.equals("U")){
			up.paintIcon(this, g, snakex[0], snakey[0]);
		}else if(direction.equals("D")){
			down.paintIcon(this, g, snakex[0], snakey[0]);
		}
		//画蛇身体
		for(int i=1;i<len;i++) {
			body.paintIcon(this, g, snakex[i], snakey[i]);
		}
		
		if(!isStarted) {
			g.setColor(Color.WHITE);
			g.setFont(new Font("arial",Font.BOLD,30));
			g.drawString("Press Space To Start/Pause！", 250, 250);
		}
		
		if(isFailed) {
			g.setColor(Color.WHITE);
			g.setFont(new Font("arial",Font.BOLD,30));
			g.drawString("Game Over,Press Space To Restart！", 250, 250);
		}
		food.paintIcon(this, g, foodx, foody);
		g.setColor(Color.WHITE);
		g.setFont(new Font("arial",Font.PLAIN,10));
		g.drawString("Score: " + score ,800,30);
		g.drawString("Len: " + len ,800,50);
		
	}
	
	public void setup() {
		isStarted = false;
		isFailed = false;
		 keyStop = true;
		 direction = "R";
		 timer.start();
		len = 3;
		snakex[0] = 100;
		snakey[0] = 100;
		snakex[1] = 75;
		snakey[1] = 100;
		snakex[2] = 50;
		snakey[2] = 100;
		
	}

	
	@Override
	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub	
		int keyCode = e.getKeyCode();
		if(keyCode == KeyEvent.VK_SPACE) {
			keyStop = !keyStop;
			if(isFailed) {
				setup();    
			}else {
				isStarted = !isStarted;
			}
		}
		
		if(!keyStop) {
			if(keyCode == KeyEvent.VK_UP && direction != "D") {
				direction = "U";
			}else if(keyCode == KeyEvent.VK_DOWN && direction != "U") {
				direction = "D";
			}else if(keyCode == KeyEvent.VK_RIGHT && direction != "L") {
				direction = "R";
			}else if(keyCode == KeyEvent.VK_LEFT && direction != "R") {
				direction = "L";
			}
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		//定义一个闹钟
		timer.start();
		
		//移动
		if(isStarted && !isFailed) {
			//移动身体
			for(int i=len;i>0;i--) {
				snakex[i] = snakex[i-1];
				snakey[i] = snakey[i-1];
			}
			//移动头
			if(direction.equals("R")){
				snakex[0] = snakex[0] + 25;
				if(snakex[0]> 850) {
					snakex[0] = 25;
				}
			}else if(direction.equals("L")){
				snakex[0] = snakex[0] - 25;
				if(snakex[0] < 25) {
					snakex[0] = 850;
				}
			}else if(direction.equals("U")){
				snakey[0] = snakey[0] - 25;
				if(snakey[0] <75) {
					snakey[0] = 650 ;
				}
			}else if(direction.equals("D")){
				snakey[0] = snakey[0] + 25;
				if(snakey[0] > 650) {
					snakey[0] = 75;
					}
			}
			//吃食物
			if(snakex[0] == foodx && snakey[0] == foody) {
				len++; 
				score = (len - 3 )*10;
				foodx = rand.nextInt(34)*25 + 25;
				foody =rand.nextInt(24)*25 +75;  
			}
		
			for(int i=1;i<len;i++){
				if(snakex[0] == snakex[i] && snakey[0] == snakey[i]) {
					isFailed = true;
				}
			}
	}
		
		//更新画面
		repaint();
	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub	
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub	
	}
}
	
